package vip.zhenzicheng.pool;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

/**
 * 定时任务线程池
 *
 * @author zhenzicheng
 * @date 2023-03-12 12:24
 */
public class ScheduleTimer {
  public static void main(String[] args) {
    ScheduledExecutorService scheduledThreadPool = Executors.newScheduledThreadPool(5);
    for (int i = 0; i < 2; i++) {
      // 不需要传递周期时间,只执行一次
      // scheduledThreadPool.schedule(new Task("task-" + i), 0, TimeUnit.SECONDS);
      // 固定间隔
      // scheduledThreadPool.scheduleAtFixedRate(new Task("task-" + i), 0, 2, TimeUnit.SECONDS);
      // 上次任务执行结束后固定延迟执行
      scheduledThreadPool.scheduleWithFixedDelay(new Task("task-" + i), 0, 2, TimeUnit.SECONDS);
    }
  }
}

class Task implements Runnable {
  private String name;

  Task(String name) {
    this.name = name;
  }

  @Override
  public void run() {

  }
}